import java.util.Scanner;

public class PasswordSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        while (choice != 3) {
            System.out.println("请选择操作：");
            System.out.println("1. 加密密码");
            System.out.println("2. 解密密码");
            System.out.println("3. 退出系统");
            choice = scanner.nextInt();
            switch (choice) {
                case 1 -> {
                    System.out.println("请输入要加密的密码：");
                    scanner.nextLine();
                    String encrypt = scanner.nextLine();
                    String encryptPassword = encryptPassword(encrypt);
                    System.out.println("加密后的密码为：" + encryptPassword);
                }
                case 2 -> {
                    System.out.println("请输入要解密的密码：");
                    scanner.nextLine();
                    String decrypt = scanner.nextLine();
                    String decryptPassword = decryptPassword(decrypt);
                    System.out.println("解密后的密码为：" + decryptPassword);
                }
                case 3 -> {
                    System.out.println("已退出系统，谢谢使用！");
                }
                default -> {
                    System.out.println("请输入正确的选项！");
                }
            }
        }
    }

    // 加密
    private static String encryptPassword(String password) {
        StringBuilder ePassword = new StringBuilder();
        char[] chars = password.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            int encryptedChar = (int) chars[i] + i + 1 + 3;
            ePassword.append((char)encryptedChar);
        }

        // 将密码的第一位和最后一位调换顺序
        char temp = ePassword.charAt(0);
        ePassword.setCharAt(0, ePassword.charAt(ePassword.length() - 1));
        ePassword.setCharAt(ePassword.length() - 1, temp);

        return ePassword.reverse().toString();
    }

    // 解密
    private static String decryptPassword(String ePassword) {
        StringBuilder dPassword = new StringBuilder(ePassword).reverse();
        char[] chars = dPassword.toString().toCharArray();
        dPassword.setLength(0);

        for (int i = 0; i < chars.length; i++) {
            int decryptedChar = (int) chars[i] - i - 1 - 3;
            dPassword.append((char)decryptedChar);
        }
        return dPassword.toString();
    }
}

